//
//  MeatSearchTableViewCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/1.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatSearchTableViewCell.h"
#import "MeatSearchItemView.h"

#define mIconW 11.f

@interface MeatSearchTableViewCell()


/**
 店铺Icon
 */
@property (nonatomic, strong) UIImageView *shopIcon;

/**
 店铺名称
 */
@property (nonatomic, strong) UILabel *shopNameLab;


/**
 评分
 */
@property (nonatomic, strong) UILabel *scoreLab;


/**
 灰色星
 */
@property (nonatomic, strong) UIView *grayView;


/**
 
 黄色星
 */
@property (nonatomic, strong) UIView *yellowView;


/**
 店铺销量
 */
@property (nonatomic, strong) UILabel *shopSalesLab;


/**
 Btn
 */
@property (nonatomic, strong) UIButton *checkBtn;


/**
 肉部位1
 */
@property (nonatomic, strong) MeatSearchItemView *itemView_1;


/**
 肉部位2
 */
@property (nonatomic, strong) MeatSearchItemView *itemView_2;


/**
 肉部位3
 */
@property (nonatomic, strong) MeatSearchItemView *itemView_3;


@end


@implementation MeatSearchTableViewCell

#pragma mark - lifecycle methods

- (void)setupUI{
    
    [self addSubview:self.shopIcon];
    [self addSubview:self.shopNameLab];
    [self addSubview:self.checkBtn];
    [self addSubview:self.shopSalesLab];
    [self addSubview:self.scoreLab];
    [self addSubview:self.grayView];
    [self addSubview:self.yellowView];
    [self addSubview:self.itemView_1];
    [self addSubview:self.itemView_2];
    [self addSubview:self.itemView_3];
}


- (void)setupLayout{
    
    {
        _shopIcon.size = CGSizeMake(52, 52);
        _shopIcon.mj_x = 10;
        _shopIcon.mj_y = 15;
    }
    
    
    {
        _shopNameLab.size = CGSizeMake(KScreenWidth/3.0, 21);
        _shopNameLab.mj_x = _shopIcon.right + 15;
        _shopNameLab.mj_y = 19;
    }
    
    
    {
        _grayView.mj_x = _shopNameLab.left;
        _grayView.mj_y = _shopNameLab.bottom + 7;
    
        _yellowView.mj_x = _grayView.mj_x;
        _yellowView.mj_y = _grayView.mj_y;
    }
    
    {
        _scoreLab.mj_x = _grayView.right + 5;
        _scoreLab.mj_y = 45;
        _scoreLab.width = 20;
        _scoreLab.height = 17;
    }
    
    {
        _shopSalesLab.mj_x = _scoreLab.right + 2;
        _shopSalesLab.mj_y = 45;
        _shopSalesLab.width = 100;
        _shopSalesLab.height = 17;
    }

    
    {
        _checkBtn.size = CGSizeMake(56, 27);
        _checkBtn.mj_x = KScreenWidth - 30 - _checkBtn.width - 10;
        _checkBtn.mj_y = 28;
    }
    
    {
        _itemView_1.mj_y = _itemView_2.mj_y = _itemView_3.mj_y = _shopIcon.bottom + 15;
        _itemView_1.mj_x = 10;
        _itemView_2.mj_x = _itemView_1.right+7;
        _itemView_3.mj_x = _itemView_2.right +7;
    }
    
}


#pragma mark - setter methods

- (void)setItem:(MeatShopSortList *)item{
    
    _shopNameLab.text = item.shopName;
    [_shopIcon  sd_setImageWithURL:[NSURL URLWithString:item.imgName] placeholderImage:[UIImage imageNamed:@"placeholder_small"]];
    _scoreLab.text = [NSString stringWithFormat:@"%@",item.starNum];
    _shopSalesLab.text = [NSString stringWithFormat:@"累计销量%@", item.saleNum];
    [self resetScoreNum:[item.starNum floatValue]];
    
    @weakify(self);
    [item.meets enumerateObjectsUsingBlock:^(MeatShopSortMeets * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        
        if (idx == 0) {
            self.itemView_1.hidden = NO;
            self.itemView_1.meet = obj;
        }
        
        if (idx == 1) {
            self.itemView_2.hidden = NO;
            self.itemView_2.meet = obj;
        }
        
        if (idx == 2) {
            self.itemView_3.hidden = NO;
            self.itemView_3.meet = obj;
        }
        
    }];
}

- (void)resetScoreNum:(CGFloat)scoreNum{
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    
    NSInteger aims = scoreNum/0.5;
    NSInteger index = aims/2;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, IconW *scoreNum + IconG*index, IconW)];
    maskLayer.path = path.CGPath;
    self.yellowView.layer.mask = maskLayer;
}


#pragma mark - getter methods

- (UIImageView *)shopIcon{
    if (!_shopIcon) {
        _shopIcon = [[UIImageView alloc] init];
        [self addShadowAndCircleCorner:_shopIcon.layer corner:26];
        _shopIcon.image = [UIImage imageNamed:@"placeholder_small"];
    }
    return _shopIcon;
}

//店铺名称
- (UILabel *)shopNameLab{
    
    if (!_shopNameLab) {
        _shopNameLab = [[UILabel alloc] init];
        _shopNameLab.textColor = UIColorFromRGB(0x262626);
        _shopNameLab.font = mMediumFont(16);
    }
    return _shopNameLab;
}


- (UILabel *)scoreLab{
    
    if (!_scoreLab) {
        _scoreLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0xFDC60A) font:mFont(12) textAliment:0];
    }
    return _scoreLab;
}


- (UIView *)grayView{
    
    if (!_grayView) {
        _grayView = [[UIView alloc] init];
        _grayView.size = CGSizeMake(mIconW *5 + IconG *4, mIconW);
        
        for (NSInteger i = 0; i< 5; i++) {
            UIImageView *icon = [[UIImageView alloc] init];
            icon.frame = CGRectMake((mIconW+IconG)*i , 0, mIconW, mIconW);
            icon.image = [UIImage imageNamed:@"like_starRank_gray"];
            [_grayView addSubview:icon];
        }
    }
    return _grayView;
}


- (UIView *)yellowView{
    
    if (!_yellowView) {
        _yellowView = [[UIView alloc] init];
        _yellowView.size = CGSizeMake(mIconW *5 + IconG *4, mIconW);
        
        for (NSInteger i = 0; i< 5; i++) {
            UIImageView *icon = [[UIImageView alloc] init];
            icon.frame = CGRectMake((mIconW+IconG)*i, 0, mIconW, mIconW);
            icon.image = [UIImage imageNamed:@"like_starRank_yellow"];
            [_yellowView addSubview:icon];
        }
    }
    return _yellowView;
}


//店铺销量
- (UILabel *)shopSalesLab{
    
    if (!_shopSalesLab) {
        _shopSalesLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x999999) font:mFont(12) textAliment:0];
    }
    return _shopSalesLab;
}


- (UIButton *)checkBtn{
    
    if (!_checkBtn) {
        _checkBtn = [UIButton makeButtonWithText:@"进店铺" textColor:KWhiteColor font:mFont(12) textAliment:1 cornerRadius:13.5];       
        [_checkBtn setBackgroundImage:[UIImage imageNamed:@"shop_search_inputbtn"] forState:UIControlStateNormal];
       
        
        @weakify(self);
        _checkBtn.tapBlock = ^{
            @strongify(self);
            if (self.dealInputShopBlock) {
                self.dealInputShopBlock();
            }
        };
    }
    return _checkBtn;
}


- (MeatSearchItemView *)itemView_1{
    
    if (!_itemView_1) {
        _itemView_1 = [[MeatSearchItemView alloc] init];
        _itemView_1.hidden = YES;
    }
    return _itemView_1;
}


- (MeatSearchItemView *)itemView_2{
    
    if (!_itemView_2) {
        _itemView_2 = [[MeatSearchItemView alloc] init];
        _itemView_2.hidden = YES;

    }
    return _itemView_2;
}

- (MeatSearchItemView *)itemView_3{
    
    if (!_itemView_3) {
        _itemView_3 = [[MeatSearchItemView alloc] init];
        _itemView_3.hidden = YES;
    }
    return _itemView_3;
}


@end
